package org.example.blog_manage.service;

import org.example.blog_manage.mapper.IBlogInfoMapper;
import org.example.blog_manage.model.BlogInfo;
import org.example.blog_manage.model.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogInfoService {
    @Autowired
    private IBlogInfoMapper iBlogInfoMapper;


    public List<BlogInfo> selectAll(){
        return iBlogInfoMapper.selectAll();
    }


    public BlogInfo selectById(Integer id){
        return iBlogInfoMapper.selectById(id);
    }

    /*
     * 根据博客Id（博客表）查用户userid(博客表)，再根据用户userId(博客表)获取到博客信息
     * */
    public List<BlogInfo> selectByBlogId(Integer blogId){
        BlogInfo blogInfo = iBlogInfoMapper.selectById(blogId);
        Integer userId = blogInfo.getUserId();
        return iBlogInfoMapper.selectByUId(userId);
    }

    public Boolean insertBlogInfo(BlogInfo blogInfo1) {
        Integer n = iBlogInfoMapper.insertBlogInfo(blogInfo1);
        if(n==1){
            return true;
        }
        return false;
    }

    public Boolean compareUId(Integer userId, Integer blogId){
        BlogInfo blogInfo = iBlogInfoMapper.selectById(blogId);
        Integer userId1 = blogInfo.getUserId();
        if(userId1==userId){
            return true;
        }
        return false;

    }

    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        Integer n = iBlogInfoMapper.updateById(blogInfo);
        if(n==1){
            return true;
        }else {
            return false;
        }
    }

    public BlogInfo getBlogInfoDetail(Integer blogId) {
        return iBlogInfoMapper.selectById(blogId);
    }

    public Boolean update(BlogInfo blogInfo) {
        Integer n = iBlogInfoMapper.updateById(blogInfo);
        if(n==1){
            return true;
        }
        return false;
    }
}
